<template>
  <view>
    <view class="goods_list">
      <view v-for="(item , i ) in goodsList" :key="i" @click="goToShopDetail(item)">
        <my-goods :item="item"></my-goods>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        queryObj:{
          query: '',
          cid: '',
          pagenum:1,
          pagesize:10
        },
        goodsList:[],
        total:'',
       //节流阀
       isloading: false
      };
    },
    onLoad(options) {
      this.queryObj.cid = options.cid || '' ,
      this.queryObj.query = options.query || ''
       // console.log(this.queryObj);
       // console.log(options.cid);
       this.getGoodsList()
    },
    methods:{
     async getGoodsList(cb){
       //开启节流阀
       this.isloading = true
       const {data:res} = await uni.$http.get('/goods/search',this.queryObj)
       // 数据请求完毕关闭节流阀
       this.isloading = false
       //判断是否传入cb，传入的话数据获取完毕结束下拉刷新状态
       cb && cb()
       // console.log(res);
       if(res.meta.status !== 200) {
        return uni.$showMsg()
       }
       this.total = res.message.total
       this.goodsList =[...this.goodsList, ...res.message.goods]
       
      },
      goToShopDetail(item){
        uni.navigateTo({
          url:'/subpkg/goods-detail/goods-detail?goods_id=' + item.goods_id
        })
      }
    },
    //上拉触底事件
    onReachBottom() {
      // console.log('ok');
      if(this.isloading === true) return
      if(this.queryObj.pagenum*this.queryObj.pagesize >= this.total) return uni.showToast({
        title:'数据加载完毕',
        icon:'none'
      })
      this.queryObj.pagenum +=1
      this.getGoodsList()
    },
    //下拉刷新事件
    onPullDownRefresh() {
      this.queryObj.pagenum = 1
      this.total = 0
      this.isloading = false
      this.goodsList = []
      //重新调用getGoodsList方法，传入结束刷新等待的回调函数
      this.getGoodsList(() => uni.stopPullDownRefresh())
    }
  }
</script>

<style lang="scss">

</style>
